const fs = require('fs')
const path = require('path')
const router = require('koa-router')()
const common = require("../utils/common")
const makeAllData = require("../utils/makeAllData")


router.get('/', async (ctx, next) => {
  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})

router.get('/getDataTop10', async (ctx, next) => {
  ctx.body = await common.getData()
})

router.get('/makeAllData', async (ctx, next) => {
  global.max_page = 320
  makeAllData.main()
  ctx.body = "正在制造"
})
// 只拿三页的数据
router.get('/makeTodayAllData', async (ctx, next) => {
  global.max_page = 3
  makeAllData.main()
  ctx.body = "正在制造 只取3页数据"
})

// 获取已经生成的所有数据的json
router.get('/getAllDataList', async (ctx, next) => {
  let p_2 = path.resolve(__dirname, "../", "public", "allData", "total.json")
  ctx.body = fs.readFileSync(p_2, "utf-8")
})

// router.get('/getAllData', async (ctx, next) => {
//   ctx.body = await common.getData()
// })

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})

module.exports = router

// 每小时刷一次数据
setInterval(() => {
  global.max_page = 3
  makeAllData.main()
}, 3600 * 1000);